package com.example.site.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.site.entity.Type;
import com.example.site.service.TypeService;
import com.example.site.mapper.TypeMapper;
import com.example.site.vo.PageVO;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author haoka
 * @description 针对表【hotel_type(房间类型)】的数据库操作Service实现
 * @createDate 2023-04-11 19:43:03
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
        implements TypeService {

    /**
     * @param
     * @return
     * @deprecated 创建房间类型
     */
    public SaResult create(Type type) {
        if (StringUtils.isBlank(type.getName())) {
            return SaResult.error("名称不能为空");
        } else if (StringUtils.isBlank(type.getPhoto())) {
            return SaResult.error("封面不能为空");
        } else {
            boolean result = this.save(type);
            if (result) {
                return SaResult.ok();
            } else {
                return SaResult.error("创建失败");
            }
        }
    }

    /**
     * @param data
     * @return
     * @deprecated 房间类型查询
     */
    public SaResult getList(Map<String, String> data) {
        String name = data.get("name");
        String _page = data.getOrDefault("page", "1");
        String _page_size = data.getOrDefault("page_size", "20");
        Page<Type> page = new Page<>(Integer.parseInt(_page), Integer.parseInt(_page_size));

        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(Type::getName, name);
        }

        this.page(page, queryWrapper);
        PageVO<Type> pageVO = new PageVO<>(page);
        return SaResult.data(pageVO);
    }

    /**
     * @param type
     * @return
     * @deprecated 编辑
     */
    public SaResult edit(Type type) {
        boolean result = this.updateById(type);
        if (result) {
            return SaResult.ok();
        } else {
            return SaResult.error("修改失败");
        }
    }


    /**
     * @param
     * @return
     * @deprecated 获取所有房间
     */
    public SaResult getAll() {
        return SaResult.data(this.list());
    }

    /**
     * 获取房间详情
     */
    public SaResult getDetail(Integer id) {
        Type type = this.getById(id);
        if (type != null) {
            return SaResult.data(type);
        } else {
            return SaResult.error("未找该房间类型").setCode(404);
        }
    }
}




